package com.twu.biblioteca;

import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {
	
	private Library library;
    private UserManager userManager;
    private boolean loggedInStatus = false;
    private User loggedInUser;
	
	public BibliotecaApp(Library library, UserManager userManager) {
        this.library = library;
        this.userManager = userManager;
	}

    public String getWelcomeMessage() {
		return "Welcome to Biblioteca";
	}

	public String[] getOptions() {
		String options[] = {"List Books","Reserve Book","List Movies","Reserve Movie","Check Membership","Login/Logout","Exit"};
		return options;
	}
	
	public void showBookList(){
		List<Book> bookList = library.getBookList();
        if(bookList.size()==0){
            System.out.println("No Book Found.");
            return;
        }
        System.out.println("List of available books :");
		for(int i=0;i<bookList.size();i++){
			System.out.println((i+1) + ". " + bookList.get(i));
		}
	}
	
	public String reserveBook(String bookISBNNumber, String customerName){
		if(library.reserveBook(bookISBNNumber, customerName)){
			return "Thank You! Enjoy the book.";
		}else{
			return "Sorry we don't have that book yet.";
		}
	}

    public String reserveMovie(String movieID, String customerName){
        if(library.reserveMovie(movieID, customerName)){
            return "Thank You! Enjoy the movie.";
        }else{
            return "Sorry we don't have that movie yet.";
        }
    }
	
	public String checkMembership(){
        if(isLoggedIn())
            return loggedInUser.toString();
		return "Please talk to a Librarian. Thank you.";
	}
	
	public void executeOption(int option){
        Scanner in = new Scanner(System.in);
		switch(option){
		case 1:
			showBookList();
			break;
		case 2:
            if(!checkForAuthentication())
                break;
			System.out.println("Enter Book ISBN Number :");
			String bookISBNNumber = in.nextLine();
            System.out.println(reserveBook(bookISBNNumber, loggedInUser.getUserName()));
			break;
         case 3:
             showMoviesList();
             break;
		case 4:
            if(!checkForAuthentication())
                break;
            System.out.println("Enter Movie ID :");
            String movieID = in.nextLine();
            System.out.println(reserveMovie(movieID, loggedInUser.getUserName()));
            break;

        case 5:
			System.out.println(checkMembership());
			break;
		case 6:
            if(isLoggedIn())
                logout();
            else {
                System.out.println("Enter UserID:");
                String userId = in.nextLine();
                System.out.println("Enter Password:");
                String password = in.nextLine();
                if(login(userId, password)){
                    System.out.println("\nYou Logged In successfully.\n");
                }else {
                    System.out.println("\nWrong UserId/Password.\n");
                }
            }
			break;
        case 7:
            System.exit(0);
		default:
			System.out.println("Select a valid option!!");
		}
	}

    private void logout() {
        loggedInStatus = false;
        loggedInUser = null;
        System.out.println("You are logged out.");
    }

    private boolean login(String userId, String password) {
        if(!userManager.login(userId, password)){
            return false;
        }
        else {
            loggedInStatus = true;
            loggedInUser = userManager.getUserById(userId);
            return true;
        }
    }

    private void showMoviesList() {
        List<Movie> movieList = library.getMovieList();
        if(movieList.size()==0){
            System.out.println("No Movie Found.");
            return;
        }
        System.out.println("Movie - Year - Director - Rating");
        for(int i=0;i<movieList.size();i++){
            System.out.println(movieList.get(i));
        }
    }

    private boolean isLoggedIn(){
        return loggedInStatus;
    }

    private boolean checkForAuthentication(){
        if(!isLoggedIn()){
            System.out.println("Please Login.");
            return false;
        }
        return true;
    }

    public static void main(String arg[]){
        Library library = Library.buildLibrary();
        try {
            DataLoader dataLoader = DataLoader.buildDataLoader();
            dataLoader.loadBookData(library);
            dataLoader.loadMovieData(library);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }

        UserManager userManager = new UserManager();
        userManager.createUser("pass1", new UserDetails("Arun","arun@gmail.com",12345678));
        userManager.createUser("pass2", new UserDetails("Kumar","kumar@gmail.com",87654321));

		BibliotecaApp biblioteca = new BibliotecaApp(library, userManager);
		System.out.println(biblioteca.getWelcomeMessage());
		Scanner in = new Scanner(System.in);
		int option;
		do{
            System.out.println("");
			for(int i=0;i<biblioteca.getOptions().length;i++){
				System.out.println((i+1) + ". " + biblioteca.getOptions()[i]);
			}
			System.out.println("Enter your option : ");
			option = in.nextInt();
			biblioteca.executeOption(option);
		}while(option!=biblioteca.getOptions().length);
	}

}
